package edu.cuit.avatar.array;

import java.util.Arrays;

public class ArrayProcess {
    public static void main(String[] args) {
        int[] list = new int[10];

        randomInitArray(list);
        printArray(list);
        //使用数组工具类Arrays打印数组
        System.out.println(Arrays.toString(list));
        System.out.println("sum = " + sum(list));
        System.out.println("max = " + max(list));
        System.out.println("max = " + max(new int[]{-10, -20, -3, -100}));

        int array[] = new int[10];
        for (int i = 0; i < array.length; i++) {
            array[i] = i;
        }
        System.out.println(Arrays.toString(array));
        //shuffling(array);
        moveElement(array);
        System.out.println(Arrays.toString(array));
    }

    public static void randomInitArray(int[] list){
        for(int i = 0; i < list.length; i++)
            list[i] = (int)(Math.random() * 100);
    }

    /**
     * 打印数组
     * [element1, element2, ... elementn]
     * @param list 要打印的数组
     */
    public static void printArray(int[] list){
        System.out.print("[");
        for (int i = 0; i < list.length; i++) {
            System.out.print(list[i] + (i == list.length - 1 ? "]\r\n" : ", "));
        }
    }

    public static int sum(int[] list){
        int total = 0;
//        for (int i = 0; i < list.length; i++) {
//            total += list[i];
//        }
        for(int e : list)
            total += e;
        return total;
    }

    public static int max(int[] list){
        int max = list[0];
        for(int i = 1; i < list.length; i++){
            if(max < list[i])
                max = list[i];
        }
        return max;
    }

    public static void shuffling(int[] list){
        for(int i = 0; i < list.length; i++){
            int j = (int)(Math.random() * list.length);
            int temp = list[i];
            list[i] = list[j];
            list[j] = temp;
        }
    }

    public static void moveElement(int[] list){
        int first = list[0];
        for(int i = 1; i < list.length; i++)
            list[i - 1] = list[i];
        //将第一个元素放入数组的结尾
        list[list.length - 1] = first;
    }
}
